package icu.sehnen.user.controller;



import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import icu.sehnen.common.result.CommonResult;
import icu.sehnen.user.entity.Role;
import icu.sehnen.user.mapper.RolePermissionRelationMapper;
import icu.sehnen.user.service.IRolePermissionRelationService;
import icu.sehnen.user.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author icu.sehnen
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/user/role")
public class RoleController {
    @Autowired
    IRoleService roleService;
    @Autowired
    RolePermissionRelationMapper rolePermissionRelationMapper;
    @GetMapping("/list")
    public CommonResult getRoleList() {
        List<Role> list = roleService.getRoleList();
        return CommonResult.success(list);
    }
    @GetMapping("/findAll")
    public CommonResult getAll() {
        List<Role> list = roleService.list();
        return CommonResult.success(list);
    }

    @PostMapping("/update")
    public CommonResult updateRole(Role role) {
        Boolean res=roleService.updateById(role);
        if(res){
            return   CommonResult.success();
        }
        return  CommonResult.failed();
    }
    @PostMapping("/add")
    public CommonResult addRole(Role role){
        Boolean res=roleService.save(role);
        if(res){
            return CommonResult.success();
        }
        return  CommonResult.failed();
    }
    @GetMapping("/find/{id}")
    public CommonResult findRoleById(@PathVariable Long id){
        Role role=roleService.getRoleById(id);
        return CommonResult.success(role);
    }
    @PostMapping("/delete/{id}")
    public CommonResult deleteRole(@PathVariable Long id){
        Boolean res=roleService.removeById(id);
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("role_id",id);
        rolePermissionRelationMapper.delete(updateWrapper);
        if (res) return CommonResult.success();
    return CommonResult.failed();
    }


}
